/*
 *  Description: BOAT XU SOFT FHSS
 *  Version: V1.00
 *  Author: boat xu
 *  Email: xifengzui@outlook.com
 *  Date: 2020-07-10 17:00:40
 *  LastEditTime: 2020-07-22 15:36:05
 */ 
#ifndef __BSF_PAYLOAD_H__
#define __BSF_PAYLOAD_H__
#include "bsf_types.h"
#include "bsf_itypes.h"

//无论什么编译器均以1个字节对齐
#if defined(WIN32) || defined(WIN64)
#pragma pack(1)
#endif

#define MAGIC_NUM_1 'B'
#define MAGIC_NUM_2 'X'

#define BSF_PACKSIZE 64
#define BSF_PAYLOADSIZE (BSF_PACKSIZE - sizeof(bsf_PayloadHead_Struct))

#define TX_PAYLOAD_BUF_COUNT 100
#define RX_PAYLOAD_BUF_COUNT 100

#define TPM_USED 0xAA
#define TPM_FREE 0x55

#ifndef b_payloadTypes
typedef enum
{
    b_pt_sync,
    b_pt_seedTable,
    b_pt_userData,
    b_pt_max,
}payloadTypes;


#endif

typedef struct //8byte
{   
    b_u8_t magicNumber[2];//magic number must match
    payloadTypes payloadType;//payload的内容类型，一般从机不发送sync包，只发送数据包
    b_u8_t totalSize;//total size of head and pdata;
}bsf_PayloadHead_Struct;

typedef struct 
{
    bsf_PayloadHead_Struct head;
    b_u8_t payload[BSF_PAYLOADSIZE];
}bsf_payload_struct;
typedef union 
{
    bsf_payload_struct stru;
    b_u8_t flatBuf[BSF_PACKSIZE];
}bsf_payload;

typedef struct 
{
    b_u8_t tStatus[TX_PAYLOAD_BUF_COUNT];
    b_u8_t rStatus[RX_PAYLOAD_BUF_COUNT];
    b_u8_t tWIndex;
    b_u8_t tRIndex;
    b_u8_t rWIndex;
    b_u8_t rRIndex;
    b_u16_t txOverWriterLoss;//写覆盖导致的丢失 
    b_u16_t rxOverWriterLoss;//收覆盖导致的丢失
}bsf_paylaod_info;

typedef struct
{
    bsf_paylaod_info info;
    bsf_payload TXPayload[TX_PAYLOAD_BUF_COUNT];
    bsf_payload RXPayload[RX_PAYLOAD_BUF_COUNT];
}bsf_paylaod_manager;
#if defined(WIN32) || defined(WIN64)
#pragma pack()
#endif
void bsf_payload_init();

b_boolean bsf_payloadPushTXBuf_Begin(bsf_payload **outBuf);
void bsf_payloadPushTXBuf_End(b_boolean isOkToPush);

b_boolean bsf_payloadPopTxBuf_Begin(bsf_payload **outBuf);
void bsf_payloadPopTxBuf_End(b_boolean isOkToPop);

b_boolean bsf_payloadPushRxBuf_Begin(bsf_payload **outBuf);
void bsf_payloadPushRxBuf_End(b_boolean isOkToPush);

b_boolean bsf_payloadPopRxBuf_Begin(bsf_payload **outBuf);
void bsf_payloadPopRxBuf_End(b_boolean isOkToPop);

void bsf_payloadInfoSet(payloadTypes payloadType,b_u8_t userDatasize,bsf_payload *inBuf);
b_boolean bsf_payloadInfoGet(payloadTypes *outTypes,b_u8_t *userDataSize,bsf_payload *inBuf);
bsf_payload * bsf_payloadTraversal(b_u8_t count);
b_u8_t bsf_payloadIsInUse(bsf_payload *pld);

b_boolean bsf_payloadInfoCheckIsOk(bsf_payload *inBuf);
#endif
